# -*- coding: utf-8 -*-

__author__ = 'lee'

import scrapy
import sys
import urlparse
from scrapy import Selector
from scrapy import Request
from Umei.items import AlbumItem

reload(sys)
sys.setdefaultencoding('utf-8')


class Umei(scrapy.Spider):
    name = "umei"
    allowed_domains = ["umei.cc"]
    start_urls = ["http://www.umei.cc/p/gaoqing/index-1.htm"]

    def parse(self, response):
        # 解析本页面
        links = Selector(response).xpath('//img[@class="img"]/..')
        for link in links:
            # print link.extract()
            # title = link.xpath('./@title').extract()[0]
            url = link.xpath('./@href').extract()[0]
            url = 'http://' + self.allowed_domains[0] + url
            yield Request(url, callback=self.parse_album)

        # 获取下一页的
        next_url = self.find_next_page(response)
        if len(next_url) > 0:
            # print next_url
            yield Request(next_url, callback=self.parse)

    def find_next_page(self, response):
        prefix = response.url[:response.url.rfind('/') + 1]
        links = Selector(response).xpath('//div[@class="pages"]/a')
        next_page = ''
        for link in links:
            next_str = link.xpath('./text()').extract()[0]
            if cmp(next_str, u"下一个") == 0 or cmp(next_str, u"下一页") == 0:
                next_page = link.xpath('./@href').extract()[0]
                break
        if len(next_page) == 0 or cmp('#', next_page) == 0:
            return
        else:
            return urlparse.urljoin(response.url, next_page)

    def parse_album(self, response):
        selector = Selector(response)
        title = selector.xpath('/html/head/title/text()').extract()[0]
        images = selector.xpath('//img[@class="IMG_show"]/@src').extract()
        next_url = self.find_next_page(response)

        try:
            last = response.meta['item']
            if last:
                for img in images:
                    last.append(img)
                images = last
        except:
            pass

        if next_url:
            yield Request(next_url, callback=self.parse_album, meta={'item': images})
        else:
            album = AlbumItem()
            album['title'] = title
            album['url'] = response.url
            album['images'] = images
            yield album
